﻿using Microsoft.Xna.Framework.Input;

namespace Bunbyx.game
{
    public class KeyboardListener
    {
        // --------------------------------------------------------------------
        //  Public attributes
        // --------------------------------------------------------------------

        public BunbyX BunbyX;

        // --------------------------------------------------------------------
        //  Private attributes
        // --------------------------------------------------------------------

        private KeyboardState previousKeyboardState;
        private PauseScreen.PauseGame pauseGameHandler;

        // --------------------------------------------------------------------
        //  Constructor
        // --------------------------------------------------------------------

        public KeyboardListener(PauseScreen.PauseGame pauseGameHandler)
        {
            this.pauseGameHandler = pauseGameHandler;
            this.previousKeyboardState = Keyboard.GetState();
        }

        // --------------------------------------------------------------------
        //  Public methods
        // --------------------------------------------------------------------

        public void Update(bool paused)
        {
            KeyboardState keyboardState = Keyboard.GetState();
            bool actionDone = false;

            // Pause
            if (keyboardState.IsKeyDown(Keys.Escape) && !previousKeyboardState.IsKeyDown(Keys.Escape))
            {
                pauseGameHandler();
            }

            if (!paused)
            {
                // Go left
                

                // Jump
                
                if (keyboardState.IsKeyDown(Keys.Up) && !previousKeyboardState.IsKeyDown(Keys.Up))
                {
                    BunbyX.DoAction(Action.GoUp);
                    actionDone = true;
                }
                if (keyboardState.IsKeyDown(Keys.Down))
                {
                    BunbyX.DoAction(Action.GoDown);
                    actionDone = true;
                }
                if (keyboardState.IsKeyDown(Keys.Down) && keyboardState.IsKeyDown(Keys.Right) && !previousKeyboardState.IsKeyDown(Keys.Right))
                {
                    BunbyX.DoAction(Action.SlideR);
                    actionDone = true;
                }
                if (keyboardState.IsKeyDown(Keys.Down) && keyboardState.IsKeyDown(Keys.Left) && !previousKeyboardState.IsKeyDown(Keys.Left))
                {
                    BunbyX.DoAction(Action.SlideL);
                    actionDone = true;
                }
                if (keyboardState.IsKeyDown(Keys.Left) && !keyboardState.IsKeyDown(Keys.Down))
                {
                    BunbyX.DoAction(Action.GoLeft);
                    actionDone = true;
                }
                // Go right
                else if (keyboardState.IsKeyDown(Keys.Right) && !keyboardState.IsKeyDown(Keys.Down))
                {
                    BunbyX.DoAction(Action.GoRight);
                    actionDone = true;
                }
                // Stop
                if (!actionDone)
                {
                    BunbyX.DoAction(Action.Stop);
                }

            }

            previousKeyboardState = keyboardState;
        }
    }
}
